<?php

namespace App\Exceptions;

use App\Services\Api\ApiService;
use Exception;
use GeniusTS\Roles\Exceptions\LevelDeniedException;
use GeniusTS\Roles\Exceptions\PermissionDeniedException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;

class Handler extends ExceptionHandler
{

    public function __construct(Container $container, ApiService $apiService)
    {
        parent::__construct($container);
        $this->apiService = $apiService;
    }

    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {


        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof \GeniusTS\Roles\Exceptions\RoleDeniedException) {
            // you can for example flash message, redirect...
            return $this->apiService->run(1002);
        }

        if ($exception instanceof LevelDeniedException){
            return $this->apiService->run(1002);
        }

        if ($exception instanceof PermissionDeniedException){
            return $this->apiService->run(1002);
        }
       /* if ($exception instanceof FatalThrowableError){
            return $this->apiService->run(1002);
        }*/

         if ($exception instanceof ModelNotFoundException){
             return $this->apiService->run(400);
         }


        return parent::render($request, $exception);
    }

    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($exception){
            return $this->apiService->run(401);
        }

       /* if ($request->expectsJson()) {
            return $this->apiService->run(401);
        }*/

        //return redirect()->guest(route('login'));
    }


}
